﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;

namespace Behaviors
{
    public class AutocompleteNotEmptyTrigger : TriggerBase<AutoCompleteBox>
    {
        /// <summary>
        /// Get our thing hooked up
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.TextChanged += new RoutedEventHandler(AssociatedObject_TextChanged);
        }

        /// <summary>
        /// Clean up!
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.TextChanged -= new RoutedEventHandler(AssociatedObject_TextChanged);
        }

        /// <summary>
        /// The text has changed. See if we are empty now? If so, fire!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void AssociatedObject_TextChanged(object sender, RoutedEventArgs e)
        {
            if (AssociatedObject.Text != null && AssociatedObject.Text.Length > 0)
            {
                InvokeActions(null);
            }
        }
    }
}
